from django.contrib import admin
from .models import SiteInfo, SiteVisit, PageVisitCount, ActionCount, Advertisement

@admin.register(SiteInfo)
class SiteInfoAdmin(admin.ModelAdmin):
    """网站信息管理"""
    list_display = ('site_name', 'contact_email', 'updated_at')
    fieldsets = (
        ('基本信息', {
            'fields': ('site_name', 'site_description', 'contact_email', 'logo'),
        }),
        ('页面内容', {
            'fields': ('about_content',),
            'classes': ('wide',),
        }),
    )
    
    def has_add_permission(self, request):
        """限制只能有一个网站信息记录"""
        if self.model.objects.exists():
            return False
        return super().has_add_permission(request)
    
    def has_delete_permission(self, request, obj=None):
        """禁止删除网站信息记录"""
        return False

@admin.register(SiteVisit)
class SiteVisitAdmin(admin.ModelAdmin):
    """网站访问记录管理"""
    list_display = ('page', 'ip_address', 'visited_at')
    list_filter = ('visited_at', 'page')
    search_fields = ('page', 'ip_address', 'user_agent')
    date_hierarchy = 'visited_at'
    readonly_fields = ('page', 'ip_address', 'user_agent', 'referrer', 'visited_at')
    list_per_page = 30
    
    def has_add_permission(self, request):
        """禁止手动添加访问记录"""
        return False
    
    def has_change_permission(self, request, obj=None):
        """禁止修改访问记录"""
        return False

@admin.register(PageVisitCount)
class PageVisitCountAdmin(admin.ModelAdmin):
    """页面访问计数管理"""
    list_display = ('content_type', 'object_id', 'visit_count', 'updated_at')
    list_filter = ('content_type', 'created_at')
    search_fields = ('page_path',)
    readonly_fields = ('content_type', 'object_id', 'page_path', 'visit_count', 'created_at', 'updated_at')
    list_per_page = 30
    
    def has_add_permission(self, request):
        """禁止手动添加访问计数"""
        return False
    
    def has_change_permission(self, request, obj=None):
        """禁止修改访问计数"""
        return False

@admin.register(ActionCount)
class ActionCountAdmin(admin.ModelAdmin):
    """用户操作计数管理"""
    list_display = ('action_type', 'object_id', 'count', 'updated_at')
    list_filter = ('action_type', 'created_at')
    readonly_fields = ('action_type', 'object_id', 'count', 'created_at', 'updated_at')
    list_per_page = 30
    
    def has_add_permission(self, request):
        """禁止手动添加操作计数"""
        return False
    
    def has_change_permission(self, request, obj=None):
        """禁止修改操作计数"""
        return False

@admin.register(Advertisement)
class AdvertisementAdmin(admin.ModelAdmin):
    """广告管理"""
    list_display = ('title', 'is_active', 'click_count', 'display_order', 'created_at')
    list_filter = ('is_active', 'created_at')
    search_fields = ('title', 'description')
    readonly_fields = ('click_count', 'created_at')
    list_editable = ('is_active', 'display_order')
    list_per_page = 20
    
    fieldsets = (
        ('基本信息', {
            'fields': ('title', 'description', 'image', 'url'),
        }),
        ('显示设置', {
            'fields': ('is_active', 'display_order'),
        }),
        ('统计信息', {
            'fields': ('click_count', 'created_at'),
            'classes': ('collapse',),
        }),
    )
